package controller

import (
	backend "AccessAdmin/api/backend"
	"AccessAdmin/internal/model"
	"AccessAdmin/internal/service"
	"context"
	"encoding/base64"
	"github.com/gogf/gf/v2/errors/gcode"
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/os/gfile"
	"github.com/gogf/gf/v2/os/gtime"
	"github.com/gogf/gf/v2/util/grand"
	"io"
	"strconv"
	"strings"
	"time"
)

// 首页接口
var Index = cIndex{}

type cIndex struct{}

func (c *cIndex) Index(ctx context.Context, req *backend.IndexReq) (res *backend.IndexRes, err error) {
	service.View().Render(ctx, model.View{
		Title:    "主页",
		MainTpl:  "index.html",
		NoLayout: true,
	})
	return
}

func (c *cIndex) Home(ctx context.Context, req *backend.HomeReq) (res *backend.HomeRes, err error) {
	service.View().Render(ctx, model.View{
		Title: "Home页",
	})
	return
}

func (c *cIndex) Theme(ctx context.Context, req *backend.ThemeReq) (res *backend.ThemeRes, err error) {
	service.View().Render(ctx, model.View{
		Title:    "主题",
		NoLayout: true,
	})
	return
}

func (c *cIndex) Note(ctx context.Context, req *backend.NoteReq) (res *backend.NoteRes, err error) {
	service.View().Render(ctx, model.View{
		Title:    "便签",
		NoLayout: true,
	})
	return
}

// UploadFile 上传文件
func (c *cIndex) UploadFile(ctx context.Context, req *backend.UploadFileReq) (res *backend.UploadFileRes, err error) {
	res = &backend.UploadFileRes{}
	r := g.RequestFromCtx(ctx)
	filePath := gtime.Now().Format("Y/m/d")
	uploadDir := g.Cfg().MustGet(ctx, "setting.uploadDir").String() + "/" + filePath
	file := r.GetUploadFile("file")
	names, err := file.Save(uploadDir, true)
	if err != nil {
		return res, gerror.NewCode(gcode.CodeOperationFailed, err.Error())
	}
	res.Name = file.Filename
	res.Path = filePath + "/" + names
	res.Url = "/" + res.Path
	err = service.Cache().Set(ctx, res.Path, file.FileHeader.Header.Get("Content-Type"), 600*time.Second)
	if err != nil {
		return nil, err
	}
	return
}

// Base64File base64 转图片保存
func (c *cIndex) Base64File(ctx context.Context, req *backend.Base64UploadFileReq) (res *backend.Base64UploadFileRes, err error) {
	res = &backend.Base64UploadFileRes{}
	_i := strings.Index(req.BaseCode, ",")
	dec := base64.NewDecoder(base64.StdEncoding, strings.NewReader(req.BaseCode[_i+1:]))
	filePath := gtime.Now().Format("Y/m/d")
	uploadDir := g.Cfg().MustGet(ctx, "setting.uploadDir").String() + "/" + filePath
	name := strings.ToLower(strconv.FormatInt(gtime.TimestampNano(), 36)+grand.S(6)) + ".png"
	uploadFile := gfile.Join(uploadDir, name)
	newFile, err := gfile.Create(uploadFile)
	if err != nil {
		return nil, err
	}
	defer newFile.Close()
	_, err = io.Copy(newFile, dec)
	if err != nil {
		return nil, err
	}
	res.Name = name
	res.Path = filePath + "/" + name
	res.Url = "/" + res.Path
	return
}

// DeleteFile 删除文件
func (c *cIndex) DeleteFile(ctx context.Context, req *backend.DeleteFileReq) (res *backend.DeleteFileRes, err error) {
	res = &backend.DeleteFileRes{
		Flag: true,
	}
	if len(req.Urls) > 0 {
		uploadDir := g.Cfg().MustGet(ctx, "setting.uploadDir").String()
		for _, url := range req.Urls {
			path := url
			if !strings.Contains(url, uploadDir) {
				path = uploadDir + "/" + path
			}
			if gfile.IsFile(path) {
				err := gfile.Remove(path)
				if err != nil {
					return nil, err
				}
			}
		}
	}
	return
}
